//package com.example.database.demo.aop;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.AfterThrowing;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.util.Arrays;
//
///**
// * @author 梅芙雨Vanessa
// */
//@Aspect
//@Component
//public class LoggingAspect {
//
//    private final Logger log = LoggerFactory.getLogger(LoggingAspect.class);
//    @Autowired
//    private ObjectMapper objectMapper;
//
//    /**
//     * 匹配我们自己项目的repositories、service、rest端点的切面
//     */
//    @Pointcut("within(com.example.database.demo.service.Impl.*)" +
//            "||within(com.example.database.demo.controller.*)")
//    public void applicationPackagePointcut() {
//    }
//
//    /**
//     * 记录方法抛出异常的通知
//     *
//     * @param joinPoint join point for advice
//     * @param e         exception
//     */
//    @AfterThrowing(pointcut = "applicationPackagePointcut()", throwing = "e")
//    public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
//        log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(),
//                joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : "NULL");
//
//    }
//
//    /**
//     * 在方法进入和退出时记录日志的通知
//     *
//     * @param joinPoint join point for advice
//     * @return result
//     * @throws Throwable throws IllegalArgumentException
//     */
//    @Around("applicationPackagePointcut()")
//    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
//        log.info("接口入参: {}", objectMapper.writeValueAsString(joinPoint.getArgs()));
//        try {
//            Object result = joinPoint.proceed();
//            log.info("接口出参: {}", objectMapper.writeValueAsString(result));
//            return result;
//        } catch (IllegalArgumentException e) {
//            log.error("Illegal argument: {} in {}.{}()", Arrays.toString(joinPoint.getArgs()),
//                    joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
//            throw e;
//        }
//    }
//
//}
